import { getPlanListApi, addPlanApi, delPlanApi, getPlanReportApi, getPlanApi, updatePlanApi, executePlanActionApi } from '@/api/planManage';

const planManage  = {
  namespaced: true,
  state: {
    
  },
  mutations: {
    
  },
  actions: {
    // 获取运营计划
    getAllPlanList ({ state }, queryParams) {
      return new Promise((resolve, reject) => {
        getPlanListApi(queryParams)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            reject(error)
          })
      })
    },
    // 删除
    deletePlan ({commit} ,{ planId, projectId }) {
      return new Promise((resolve, reject) => {
        delPlanApi(planId, projectId)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 查询
    getPlan ({commit} ,{ id, projectId }) {
      return new Promise((resolve, reject) => {
        getPlanApi(id, projectId)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 查询
    getPlanReport ({commit} , { id, ...restData }) {
      return new Promise((resolve, reject) => {
        getPlanReportApi(id, restData)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    addPlan({commit}, {id, projectId, ...params}) {
      return new Promise((resolve, reject) => {
        let promiseObj;
        if (id) {
          promiseObj = updatePlanApi(id, projectId, params);
        } else {
          promiseObj = addPlanApi(projectId, params);
        }
        
        promiseObj
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    executePlanAction({commit}, {id, projectId, ...params}) {
      return new Promise((resolve, reject) => {        
        executePlanActionApi(id, projectId, params)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
  },
  getters: {

  }
}

export default planManage